<?php
ini_set('html_errors', 1);
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// { common vars and functions
include_once 'inc/common.php';
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : '';
$id = isset( $_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
// }

// { get current page id
if(!$id){
	if($page){ //load by name
		$r = Page::getInstanceByName($page);
		if($r && isset($r->id)) $id = $r->id;
		unset($r);
	}
	if(!$id){ //else load by special
		$special = 1;
		if(!$page){
			$r = Page::getInstanceBySpecial($special);
			if($r && isset($r->id)) $id = $r->id;
			unset($r);
		}
	}
}
// }

// find template
$d=array();
if (!file_exists(THEME_DIR.'/'.THEME.'/t/')) {
	die(__('no theme installed. please <a href="/admin/">install one</a>'));
}

$dir=new DirectoryIterator(THEME_DIR.'/'.THEME.'/t/');
foreach ($dir as $f) {
	if ($f->isDot()) {
		continue;
	}
	$n=$f->getFilename();
	if (preg_match('/\.tpl$/', $n)) {
		$d[]=preg_replace('/\.tpl$/', '', $n);
	}
}
asort($d);
$template=$d[0];
if ($template=='') {
	die(__('no template created. please create a template first'));
}
		
// load page data
if($id){
	$PAGEDATA = (isset($r) && $r) ? $r : Page::getInstance($id);
}else{
	echo '404 thing goes here';
	exit;
}

//set up template
if(file_exists(THEME_DIR.'/'.THEME.'/t/'.$PAGEDATA->template.'.tpl')){
	$template = THEME_DIR.'/'.THEME.'/t/'.$PAGEDATA->template.'.tpl';
}else if(file_exists(THEME_DIR.'/'.THEME.'/t/_default.tpl')){
	$template = THEME_DIR.'/'.THEME.'/t/_default.tpl';
}
if($template==='') die('no template created, please create a template first');

switch ($PAGEDATA->type) {
	case '0':	//normal page
		$pagecontent = $PAGEDATA->body;
	break;
	default :
	break;
}

$title = $config->siteOptions['site-title'];
$metadata = '<title>'.htmlspecialchars($title).'</title>';
$stylesheets = '<link rel="stylesheet" type="text/css" href="/admin/jquery-ui-1.9.1.custom.css">';
$stylesheets .= '<link rel="stylesheet" href="/skins/basic/c/style.css" />';

$smarty  = Core_smartySetup('pages');

$smarty->template_dir = THEME_DIR.'/'.THEME.'/t/';

$smarty->assign('pagecontent', $pagecontent);
$smarty->assign('metadata', $metadata);
$smarty->assign('stylesheets', $stylesheets);
header('Content-type: text/html; charset=utf-8');
$smarty->display('skins/basic/t/_default.tpl');




